#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 10, INF = 0x3f3f3f3f;

int n, m;
int a[110], cnt[1 << N];
vector<int> ok;
int f[2][1 << N][1 << N];

void init()
{
    for (int st = 0;st < (1 << m);st++) {
        if ((st & ((st << 1) | (st << 2) | (st >> 1) | (st >> 2))) == 0) ok.push_back(st);
        cnt[st] = __builtin_popcount(st);
    }
}

bool check(int st, int mark) { return (st & mark) == 0; }

void solve()
{
    cin >> n >> m;
    init();
    for (int i = 1;i <= n;i++)
        for (int j = 0;j < m;j++) {
            char x;cin >> x;
            a[i] |= ((x == 'H') << j);
        }
    f[0][0][0] = 0;
    for (int st = 0;st < (1 << m);st++)
        if (check(st, a[1])) f[1][st][0] = cnt[st];

    int p = 0;
    for (int i = 2;i <= n;i++) {
        for (int now : ok) if (check(now, a[i]))
            for (int pre1 : ok) if (check(pre1, a[i - 1]) && (now & pre1) == 0)
                for (int pre2 : ok) if (check(pre2, a[i - 2]) && (pre2 & pre1) == 0 && (pre2 & now) == 0)
                    f[p][now][pre1] = max(f[p][now][pre1], f[p ^ 1][pre1][pre2] + cnt[now]);
        p = p ^ 1;
    }

    p = p ^ 1;
    int ans = 0;
    for (int now : ok) for (int pre : ok) if (check(now, a[n]) && check(pre, a[n - 1]) && (now & pre) == 0) ans = max(ans, f[p][now][pre]);
    cout << ans << '\n';
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}